//
//  AddShopCarView.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/14.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//
#import "AddShopCarView.h"

@implementation AddShopCarView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        //收藏
        UIButton *collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [collectButton addTarget:self action:@selector(collectionButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [collectButton setImage:[UIImage imageNamed:@"icon_collection"] forState:UIControlStateNormal];
        [self addSubview:collectButton];
        collectButton.frame = CGRectMake(0,0,WIDTH/8,self.Height);
        [collectButton setImageEdgeInsets:UIEdgeInsetsMake(-self.Height/3,0,0,0)];
        
        UILabel *collectionLabel = [[UILabel alloc] init];
        collectionLabel.font = FONT(11);
        collectionLabel.text = @"收藏";
        [self addSubview:collectionLabel];
        collectionLabel.textColor = DEFAULT_COLOR;
        self.collectionLabel = collectionLabel;
        
        [collectionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(collectButton.mas_centerX);
            make.centerY.equalTo(collectButton.mas_centerY).offset(10);
        }];
        
        //分享
        UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareButton addTarget:self action:@selector(shareButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
        [shareButton setImage:[UIImage imageNamed:@"icon_share"] forState:UIControlStateNormal];
        [self addSubview:shareButton];
        shareButton.frame = CGRectMake(CGRectGetMaxX(collectButton.frame),0,WIDTH/8,self.Height);
        [shareButton setImageEdgeInsets:UIEdgeInsetsMake(-self.Height/3,0,0,0)];
        
        
        UILabel *shareLabel = [[UILabel alloc] init];
        shareLabel.font = FONT(11);
        shareLabel.text = @"分享";
        [self addSubview:shareLabel];
        shareLabel.textColor = DEFAULT_COLOR;
        
        [shareLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(shareButton.mas_centerX);
            make.centerY.equalTo(shareButton.mas_centerY).offset(10);
        }];
        
        for (int i = 0; i < 2; ++i) {
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(collectButton.width+collectButton.width*i,0,1,self.Height)];
            lineView.backgroundColor = [UIColor blackColor];
            lineView.alpha = 0.1;
            [self addSubview:lineView];
        }
        NSArray *arr = @[@"加入购物车",@"立即购买"];
        
        CGFloat BtnW = WIDTH/4;
        CGFloat BtnMargin = (WIDTH-WIDTH/4*3)/3;
        
        for (int i = 0; i < arr.count; ++i) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [self addSubview:button];
            
            button.layer.cornerRadius = 5;
            button.layer.masksToBounds = YES;
            button.layer.borderWidth = 0.5;
            button.layer.borderColor = DEFAULT_COLOR.CGColor;
            
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [button setTitle:[NSString stringWithFormat:@"%@",arr[i]] forState:UIControlStateNormal];
            [button setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
            
            button.tag = 2000+i;
            [button addTarget:self action:@selector(addShopBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
            if (i==1) {
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [button setBackgroundColor:DEFAULT_COLOR];
            }
            
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(BtnW);
                make.height.mas_equalTo(self.Height/1.5);
                make.centerY.equalTo(self.mas_centerY);
                make.left.equalTo(shareButton.mas_right).offset(BtnMargin+(BtnW+BtnMargin)*i);
                
            }];
            
            
        }
        
    }
    return self;
}

//收藏Click
- (void)collectionButtonDidClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(collectionDelegateBtnDidClick:)]) {
        [self.delegate collectionDelegateBtnDidClick:button];
    }
    
//    if (!button.selected) {
//        [button setImage:[UIImage imageNamed:@"icon_collection_select"] forState:UIControlStateNormal];
//    }else{
//        [button setImage:[UIImage imageNamed:@"icon_collection"] forState:UIControlStateNormal];
//    }
//    button.selected = !button.selected;
}

//分享Click
- (void)shareButtonDidClick
{
    if ([self.delegate respondsToSelector:@selector(shareDelegateBtnDidClick)]) {
        [self.delegate shareDelegateBtnDidClick];
    }
}


//立即购买 加入购物车
- (void)addShopBtnDidClick:(UIButton *)sender
{
    
    for (UIButton *tempBtn in self.subviews) {
        
        if (tempBtn.tag > 99) {
            [tempBtn setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
            [tempBtn setBackgroundColor:[UIColor whiteColor]];
            
        }
        
        if (tempBtn.tag == sender.tag) {
            
            [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [sender setBackgroundColor:DEFAULT_COLOR];
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(AddShopCarViewDidClick:)]) {
        
        [self.delegate AddShopCarViewDidClick:sender];
    }
}

@end